package com.example.piece.ui.demo

import android.os.Bundle
import android.view.View
import androidx.databinding.ViewDataBinding
import com.therouter.router.Route
import com.example.base.ext.toast
import com.example.piece.R
import com.example.piece.constant.RoutePath
import com.example.piece.databinding.TestActivityWebSocketBinding
import com.example.piece.tools.JWebSocketClient
import com.example.piece.ui.activity.Layer3Activity
import java.net.URI

/**
 * WebSocket
 */
@Route(path = RoutePath.TESTActivity.WEB_SOCKET_TEST_ACTIVITY)
class WebSocketTestActivity :  Layer3Activity<TestActivityWebSocketBinding, ViewDataBinding>() {
    override val isTransparentStatusBar: Boolean = false

    override val isDarkMode: Boolean = false
    override val centerLayoutRes: Int
        get() = R.layout.test_activity_web_socket
    override val title: String
        get() = "WebSocket测试"
    override val leftIcon1Res: Int
        get() = R.drawable.ic_back
    private val socketClient: JWebSocketClient by lazy {
        object : JWebSocketClient(URI.create("ws://ken.free.idcfengye.com/aqsc/websocket")) {

            override fun onMessage(message: String?) {
                super.onMessage(message)
                toast("WebSocket $message")
            }
        }
    }
    override fun onClickLeftIcon1(view: View) {
        super.onClickLeftIcon1(view)
        onBackPressed()
    }

    override fun initData(savedInstanceState: Bundle?) {
        super.initData(savedInstanceState)
        if (socketClient.isOpen) {
            toast("已经连接上, 无需重复连接!")
            return
        }

        try {
            socketClient.connectBlocking()
        } catch (e: InterruptedException) {
            e.printStackTrace()
        }
    }



    override fun onDestroy() {
        super.onDestroy()
    }

}